package com.rockcent.helper;

import com.rockcent.mall.lib.domain.MallCashCoupon;
import com.rockcent.mall.lib.dto.MallCashCouponDto;

import java.util.*;

/**
 * Created by zhangjh on 2016/12/19.
 */
public enum IntegrationCashCouponHelper {
    SINGLETNONE;

    public MallCashCoupon chgDtoToDomain(MallCashCouponDto vo) {
        MallCashCoupon mallCashCoupon = new MallCashCoupon();
        mallCashCoupon.setQuantity(vo.getQuantity());
        mallCashCoupon.setFaceValue(vo.getFaceValue());
        mallCashCoupon.setName(vo.getName());
        mallCashCoupon.setPicUrl(vo.getPicUrl());
        return mallCashCoupon;
    }

    /**
     * 获取13为兑换码
     *
     * @param type
     * @return
     */
    public String getExchangeCode(String type) {

        String code = null;
        String flag = null;
        UUID uuid = UUID.randomUUID();
        String subStr = uuid.toString().substring(24);
        String prefix = subStr.substring(0, 7);
        String suffix = subStr.substring(0, 6);
        if (type.equals("coupon")) {
            flag = "1";
        } else if (type.equals("cash")) {
            flag = "2";
        }
        code = prefix + flag + suffix;
        return code;

    }


    /**
     * 获取13为兑换码
     *
     * @param type
     * @return
     */
    public List<String> getExchangeCode(String type, Long count) {
        List<String> codelist = new ArrayList<>();
        for (int idx = 0; idx < count; idx++) {
            String code = getExchangeCode(type);
            codelist.add(code);
        }
        return codelist;
    }

    /**
     * 获取13为兑换码,卡券标识码
     *
     * @param type
     * @return
     */
    public Map<String, String> getExchangeCodeAndFlagCode(String type, Long count) {
        Map<String, String> codelist = new HashMap<>();
        for (int idx = 0; idx < count; idx++) {
            String exchangeCode = getExchangeCode(type);
            String flagCode = getFlagCode(type);
            codelist.put(exchangeCode, flagCode);
        }
        return codelist;
    }

    private String getFlagCode(String type) {

        String code;
        Long nowTime = System.currentTimeMillis();
        int randomNum = (int) (Math.random() * (9999 - 1000 + 1)) + 1000;
        String flag = "0";
        if (type.equals("coupon")) {
            flag = "1";
        } else if (type.equals("cash")) {
            flag = "2";
        }
        code = nowTime + flag + randomNum;
        return code;

    }
}
